%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% File:               replication_line.m
%
% Author:             Miguel R. Rueda and Sergio Ascencio
%
% Description:        Generates results in Table 12 Appendix Model 1 
%
% Language:           MATLAB R2013b (8.2.0.701) 64 Bit
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clear
path='C:/Users/mrueda/Documents/MATLAB/Party_delegates/R&R/replication/';
data=strcat(path,'panel_matlab.xls');

%Importing data
[data2012,~]=xlsread(data,'2012');
[data2009,labels]=xlsread(data,'2009');
data=[data2009;data2012];


d_s=dataset({data(:,:) labels{:}});

%Extended
varsx={'l_d_RC_others' 'l_margin' 'lcasillas' 'school_ic' 'l_diff_PRI_PAN' 'lpop' 'l_turnout' 'local' 'ldist_city' 'l_d_frac_line' ...
'l_d_RC_others_sq' 'l_margin_sq' 'lcasillas_sq' 'school_ic_sq' 'l_diff_PRI_PAN_sq' 'lpop_sq' 'l_turnout_sq' 'ldist_city_sq' ...
'l_d_RC_others_l_margin' 'l_d_RC_others_lcasillas' 'l_d_RC_others_school_ic' 'l_d_RC_others_l_diff_PRI_PAN' 'l_d_RC_others_lpop' 'l_d_RC_others_l_turnout' 'l_d_RC_others_local' 'l_d_RC_others_ldist_city' 'l_d_RC_others_l_d_frac_line' ...
'l_margin_lcasillas' 'l_margin_school_ic' 'l_margin_l_diff_PRI_PAN' 'l_margin_lpop' 'l_margin_l_turnout' 'l_margin_local' 'l_margin_ldist_city' 'l_margin_l_d_frac_line' ...
'lcasillas_school_ic' 'lcasillas_l_diff_PRI_PAN' 'lcasillas_lpop' 'lcasillas_l_turnout' 'lcasillas_local' 'lcasillas_ldist_city' 'lcasillas_l_d_frac_line' ...
'school_ic_l_diff_PRI_PAN' 'school_ic_lpop' 'school_ic_l_turnout' 'school_ic_local' 'school_ic_ldist_city' 'school_ic_l_d_frac_line' ...
'l_diff_PRI_PAN_lpop' 'l_diff_PRI_PAN_l_turnout' 'l_diff_PRI_PAN_local' 'l_diff_PRI_PAN_ldist_city' 'l_diff_PRI_PAN_l_d_frac_line' ...
'lpop_l_turnout' 'lpop_local' 'lpop_ldist_city' 'lpop_l_d_frac_line' ...
'l_turnout_local' 'l_turnout_ldist_city' 'l_turnout_l_d_frac_line' ...
'local_ldist_city' 'local_l_d_frac_line' 'ldist_city_l_d_frac_line' 'ldist_pri' 'gov_PRI' 'ldist_pan' 'gov_PAN'};

varsy={'d_RC_PRI_c' 'd_RC_PAN_c'};
varcl={'state_district'};
varcl2={'id_state'};
[Y,X,CLv]=set_design_mat(d_s,varsy,varsx,[varcl varcl2]);


%States where there is enough variation in dep. var to estimate first stage separately for each party
    states_PRI=[9 11 12 14 17 19 20 22 24 26]';
    states_PAN=[5 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 26 27 28 30]';

    CLv_PRI=CLv(:,2);
    CLv_PRI(ismember(CLv_PRI(:,1),setdiff(CLv_PRI(:,1),states_PRI)),1)=1;
    CLv_PAN=CLv(:,2);
    CLv_PAN(ismember(CLv_PAN(:,1),setdiff(CLv_PAN(:,1),states_PAN)),1)=1;
%CLv_PRI and CL_vPAN have state codes for those states where the first stage can be estimated (for each party) and a 1 for all others 

%Remember: when runnning main function without state by state fist stage,
%still include state vectors CLv_PAN and CLv_PRI, for each party

[beta,hessian,phats,LL,auxLL,Llong]=representatives_game_line(Y,X,0,CLv_PAN,CLv_PRI,1);

nReps=500;
[betas_monte_extended,se,pvals,~,~]=bootstrap_game_line(Y,X,1,beta,nReps,0,CLv_PAN,CLv_PRI,CLv(:,1));
ses=reshape(se,[length(se)/4,4]);
pvals=reshape(pvals,[length(pvals)/4,4]);
labels={'l_d_RC_others' 'l_margin' 'lcasillas' 'school_ic' 'l_diff_PRI_PAN' 'lpop' 'l_turnout' 'local' 'ldist_city'};
results=table_game_line(beta,ses,labels)
